Public class HabitatXMLParser{
XMLParserWrapper xs = new XMLParserWrapper();
XMLParserWrapper.PimsContactSearch PimsContactSearch = new XMLParserWrapper.PimsContactSearch();
public void parseContactSearchResponse(String xmlResponse){
            system.debug(LoggingLevel.DEBUG,'Entering ContactSearchTranslator:parseContactSearchResponse:'+xmlResponse);
            DOM.Document doc = null;      
            DOM.XMLNode root = null;
            List<Dom.XMLNode> ContactSearchResponseChildList = null;
          
              
              try
               {
                  doc = new DOM.Document();
                  doc.load(xmlResponse); //Loading XML
                  root = doc.getRootElement(); //In root element
                  ContactSearchResponseChildList = root.getChildElements();
                  //PimsContactSearch = new XMLParserWrapper.PimsContactSearch();
                  List<XMLParserWrapper.RelationShipDetail> llst = new List<XMLParserWrapper.RelationShipDetail>();
                  for(Integer i= 0; i < ContactSearchResponseChildList.size(); i++)
                  {
                      Dom.XMLNode ContactSearchResponseChild = ContactSearchResponseChildList.get(i);
                      // Contact
                      if(ContactSearchResponseChild.getName() == 'Contact') {                    
                            PimsContactSearch.ContactDetailList = parseContactList(ContactSearchResponseChild);                   
                      }
                      //Address
                      if(ContactSearchResponseChild.getName() == 'Address') {                    
                            PimsContactSearch.AddressDetailList = ParseAddressList(ContactSearchResponseChild); 
                  }
                   //Email
                      if(ContactSearchResponseChild.getName() == 'Email') {                    
                            PimsContactSearch.EmailDetailList = ParseEmailList(ContactSearchResponseChild); 
                  }
                  //Telephone
                      if(ContactSearchResponseChild.getName() == 'Telephone') {                    
                            PimsContactSearch.TelephoneDetailList = ParseTelephoneList(ContactSearchResponseChild); 
                  }
                  //Relationship
                      if(ContactSearchResponseChild.getName() == 'Relationship') {                          
                             XMLParserWrapper.RelationShipDetail  rs = new  XMLParserWrapper.RelationShipDetail();
                             rs = ParseRelationshipList(ContactSearchResponseChild); 
                             if(rs != null)                            
                              llst.add(rs);       
                                                   
                  }                    
                  }
                  
                  if(llst != null && llst.SIZE()>0)
                  PimsContactSearch.RelationShipDetailList = llst;
               
                 system.debug(LoggingLevel.DEBUG,'ContactDetailList -- >:'+PimsContactSearch.ContactDetailList ); 
                 system.debug(LoggingLevel.DEBUG,'AddressDetailList -- >:'+PimsContactSearch.AddressDetailList ); 
                 system.debug(LoggingLevel.DEBUG,'EmailDetailList -- >:'+PimsContactSearch.EmailDetailList ); 
                 system.debug(LoggingLevel.DEBUG,'TelephoneDetailList -- >:'+PimsContactSearch.TelephoneDetailList );  
                 system.debug(LoggingLevel.DEBUG,'RelationshipDetailList -- >:'+PimsContactSearch.RelationShipDetailList);  
                   
               } catch (System.XMLException e) {
                  system.debug(LoggingLevel.ERROR,'XML Exception when Parsing XML response :'+e.getMessage());
              }
              system.debug(LoggingLevel.DEBUG,'Exiting PimsContactSearch -- >:'+PimsContactSearch);
              
              InsertWrapperRecords(PimsContactSearch);
              //return PimsContactSearch;
 }
 
private List<XMLParserWrapper.ContactDetail> parseContactList(Dom.XMLNode node) {
            system.debug(LoggingLevel.DEBUG,'Entering ServiceSearchTranslator:parseContactList:');
            List<Dom.XMLNode> contactNodeList=null;
            List<XMLParserWrapper.ContactDetail> ContactDetailList =null;
            try{
                contactNodeList = node.getChildElements();            
                ContactDetailList = new List<XMLParserWrapper.ContactDetail>();            
                XMLParserWrapper.ContactDetail ContactDetail = new XMLParserWrapper.ContactDetail();
                       
                for(Integer i= 0; i < contactNodeList.size(); i++) {                                  
                      Dom.XMLNode statusNode = contactNodeList.get(i);
                   
                            if (statusNode.getName().equalsIgnoreCase('partner_id')) {                      
                                  ContactDetail.partner_id = string.valueof(statusNode.getText());
                            }                       
                            if (statusNode.getName().equalsIgnoreCase('name_id')) {                        
                                  ContactDetail.name_id = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('first_name')) {                       
                                  ContactDetail.first_name = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('middle_name')) {                         
                                  ContactDetail.middle_name = string.valueof(statusNode.getText());
                            }  
                            if (statusNode.getName().equalsIgnoreCase('last_name')) {
                                  ContactDetail.last_name = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('preferred_name')) {
                                  ContactDetail.preferred_name = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('gender')) {
                                  ContactDetail.gender = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('sts')) {
                                  ContactDetail.sts = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('related_partner_id')) {
                                  ContactDetail.related_partner_id = string.valueof(statusNode.getText());
                            } 
                                          
                    } 
                    ContactDetailList.add(ContactDetail);                 
                    system.debug('ContactDetailList---->'+ContactDetailList);
            }catch(Exception e){
                system.debug(LoggingLevel.ERROR,'Error when parsing Status list:'+e.getMessage()); 
            }
            system.debug(LoggingLevel.DEBUG,'Exiting ContractSearchTranslator:parseContactList:'+ContactDetailList);
      return ContactDetailList;
      }
private List<XMLParserWrapper.AddressDetail> ParseAddressList(Dom.XMLNode node) {
            system.debug(LoggingLevel.DEBUG,'Entering ServiceSearchTranslator:ParseAddressList:');
            List<Dom.XMLNode> contactNodeList=null;
            List<XMLParserWrapper.AddressDetail> AddressDetailList  =null;
            try{
                contactNodeList = node.getChildElements();            
                AddressDetailList  = new List<XMLParserWrapper.AddressDetail>();            
                XMLParserWrapper.AddressDetail AddressDetail = new XMLParserWrapper.AddressDetail();
                     
                for(Integer i= 0; i < contactNodeList.size(); i++) {                                  
                      Dom.XMLNode statusNode = contactNodeList.get(i);
                   
                            if (statusNode.getName().equalsIgnoreCase('partner_id')) {                      
                                  AddressDetail.partner_id = string.valueof(statusNode.getText());
                            }                       
                            if (statusNode.getName().equalsIgnoreCase('address_type')) {                        
                                  AddressDetail.address_type = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('preferred_address')) {                       
                                  AddressDetail.preferred_address = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('address_line')) {                         
                                  AddressDetail.address_line = string.valueof(statusNode.getText());
                            }  
                            if (statusNode.getName().equalsIgnoreCase('city')) {
                                  AddressDetail.city = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('state')) {
                                  AddressDetail.state = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('country')) {
                                  AddressDetail.country = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('zip_code')) {
                                  AddressDetail.zip_code = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('county')) {
                                  AddressDetail.county = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('sts')) {
                                  AddressDetail.sts = string.valueof(statusNode.getText());
                            } 
                                            
                    }
                    AddressDetailList.add(AddressDetail);                
                    system.debug('AddressDetailList---->'+AddressDetailList);
            }catch(Exception e){
                system.debug(LoggingLevel.ERROR,'Error when parsing Status list:'+e.getMessage()); 
            }
            system.debug(LoggingLevel.DEBUG,'Exiting ContractSearchTranslator:ParseAddressList:'+AddressDetailList);
      return AddressDetailList;
      }
private List<XMLParserWrapper.EmailDetail> ParseEmailList(Dom.XMLNode node) {
            system.debug(LoggingLevel.DEBUG,'Entering ServiceSearchTranslator:ParseEmailList:');
            List<Dom.XMLNode> contactNodeList=null;
            List<XMLParserWrapper.EmailDetail> EmailDetailList =null;
            try{
                contactNodeList = node.getChildElements();            
                EmailDetailList = new List<XMLParserWrapper.EmailDetail>();            
                XMLParserWrapper.EmailDetail EmailDetail = new XMLParserWrapper.EmailDetail();
                       
                for(Integer i= 0; i < contactNodeList.size(); i++) {                                  
                      Dom.XMLNode statusNode = contactNodeList.get(i);
                   
                            if (statusNode.getName().equalsIgnoreCase('partner_id')) {                      
                                  EmailDetail.partner_id = string.valueof(statusNode.getText());
                            }                       
                            if (statusNode.getName().equalsIgnoreCase('email_type')) {                        
                                  EmailDetail.email_type = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('email_address')) {                       
                                  EmailDetail.email_address = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('preferred_email')) {                         
                                  EmailDetail.preferred_email = string.valueof(statusNode.getText());
                            }  
                                        
                    }
                    EmailDetailList.add(EmailDetail);                
                    system.debug('EmailDetailList---->'+EmailDetailList);
            }catch(Exception e){
                system.debug(LoggingLevel.ERROR,'Error when parsing Status list:'+e.getMessage()); 
            }
            system.debug(LoggingLevel.DEBUG,'Exiting ContractSearchTranslator:ParseEmailList:'+EmailDetailList);
      return EmailDetailList;
      }
private List<XMLParserWrapper.TelephoneDetail> ParseTelephoneList(Dom.XMLNode node) {
            system.debug(LoggingLevel.DEBUG,'Entering ServiceSearchTranslator:ParseTelephoneList:');
            List<Dom.XMLNode> contactNodeList=null;
            List<XMLParserWrapper.TelephoneDetail> TelephoneDetailList =null;
            try{
                contactNodeList = node.getChildElements();            
                TelephoneDetailList = new List<XMLParserWrapper.TelephoneDetail>();            
                XMLParserWrapper.TelephoneDetail TelephoneDetail = new XMLParserWrapper.TelephoneDetail();
                       
                for(Integer i= 0; i < contactNodeList.size(); i++) {                                  
                      Dom.XMLNode statusNode = contactNodeList.get(i);
                   
                            if (statusNode.getName().equalsIgnoreCase('partner_id')) {                      
                                  TelephoneDetail.partner_id = string.valueof(statusNode.getText());
                            }                       
                            if (statusNode.getName().equalsIgnoreCase('telephone_number')) {                        
                                  TelephoneDetail.telephone_number = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('telephone_type')) {                       
                                  TelephoneDetail.telephone_type = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('preferred_telephone')) {                         
                                  TelephoneDetail.preferred_telephone = string.valueof(statusNode.getText());
                            }  
                            if (statusNode.getName().equalsIgnoreCase('comments')) {
                                  TelephoneDetail.comments = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('country')) {
                                  TelephoneDetail.country = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('sts')) {
                                  TelephoneDetail.sts = string.valueof(statusNode.getText());
                            } 
                                       
                    }
                     TelephoneDetailList.add(TelephoneDetail);                   
                    system.debug('TelephoneDetailList---->'+TelephoneDetailList);
            }catch(Exception e){
                system.debug(LoggingLevel.ERROR,'Error when parsing Status list:'+e.getMessage()); 
            }
            system.debug(LoggingLevel.DEBUG,'Exiting ContractSearchTranslator:ParseTelephoneList:'+TelephoneDetailList);
      return TelephoneDetailList;
      }
      private XMLParserWrapper.RelationshipDetail ParseRelationshipList(Dom.XMLNode node) {
            system.debug(LoggingLevel.DEBUG,'Entering ServiceSearchTranslator:ParseRelationshipList:');
            XMLParserWrapper.RelationShipDetail RelationShipDetail = new XMLParserWrapper.RelationShipDetail();
            List<Dom.XMLNode> contactNodeList=null;
            //List<XMLParserWrapper.RelationShipDetail> RelationShipDetailList =null;
            try{
                contactNodeList = node.getChildElements();            
                //RelationShipDetailList = new List<XMLParserWrapper.RelationShipDetail>();            
                       
                for(Integer i= 0; i < contactNodeList.size(); i++) {                                  
                      Dom.XMLNode statusNode = contactNodeList.get(i);
                   
                            if (statusNode.getName().equalsIgnoreCase('partner_id')) {                      
                                  RelationShipDetail.partner_id = string.valueof(statusNode.getText());
                            }                       
                            if (statusNode.getName().equalsIgnoreCase('related_partner_id')) {                        
                                  RelationShipDetail.related_partner_id = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('related_name_id')) {                       
                                  RelationShipDetail.related_name_id = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('relationship_type')) {                         
                                  RelationShipDetail.relationship_type = string.valueof(statusNode.getText());
                            }  
                            if (statusNode.getName().equalsIgnoreCase('start_date')) {
                                  RelationShipDetail.start_date = string.valueof(statusNode.getText());
                            } 
                            if (statusNode.getName().equalsIgnoreCase('sts')) {
                                  RelationShipDetail.sts = string.valueof(statusNode.getText());
                            } 
                             //RelationShipDetailList.add(RelationShipDetail);                
                    }               
                    //system.debug('RelationShipDetailList---->'+RelationShipDetailList);
                    //system.debug('RelationShipDetailList---->'+RelationShipDetailList.size());
            }catch(Exception e){
                system.debug(LoggingLevel.ERROR,'Error when parsing Status list:'+e.getMessage()); 
            }
            system.debug(LoggingLevel.DEBUG,'Exiting ContractSearchTranslator:ParseRelationshipList:'+RelationShipDetail);
      return RelationShipDetail ;
      }
      public Id GetAccountID(String PartnerID)
      {
        Account Acccon;
        Id strAccountID = null;
        try{
            if(PartnerID != null){
                Acccon = [Select Id from Account where Partner_ID__c =: PartnerID Limit 1];
            }
        }
        catch(Exception ex){
        
        }
        If(Acccon != null){
            strAccountID = Acccon.Id;
        }
        system.debug('!!!!!!'+strAccountID);
        if(strAccountID != null){
            return strAccountID;
            }
        else{
            return null;
            }
        
      }

      public String InsertWrapperRecords(XMLParserWrapper.PimsContactSearch wrpcls){
      String Endresult;
      String PartnerId = null;
      Id ContactId;
      Id AccountId;
      If(wrpcls != null){
          If(wrpcls.ContactDetailList != null && wrpcls.ContactDetailList.size()>0){
              for(Integer K=0;K<wrpcls.ContactDetailList.size();K++){
              AccountId = GetAccountID(wrpcls.ContactDetailList[K].related_partner_id);
              }
          }
      }
      If(AccountId != null){      
          If(wrpcls != null){
          // Contact Insert
              If(wrpcls.ContactDetailList != null && wrpcls.ContactDetailList.size()>0){
              system.debug('Sample test'+wrpcls.ContactDetailList.size());
               for(Integer i=0;i<wrpcls.ContactDetailList.size();i++){
               PartnerId = wrpcls.ContactDetailList[i].partner_id;
               Contact con = new contact(Firstname = wrpcls.ContactDetailList[i].first_name,
               Lastname = wrpcls.ContactDetailList[i].last_name,
               Partner_Contact_ID__c = wrpcls.ContactDetailList[i].partner_id,
               Name_Id__c = wrpcls.ContactDetailList[i].name_id,
               Middle_Name__c = wrpcls.ContactDetailList[i].middle_name,
               Preferred_Name__c = wrpcls.ContactDetailList[i].preferred_name,
              // Gender__c = wrpcls.ContactDetailList[i].gender,
               Gender__c = Gender__c.getvalues(wrpcls.ContactDetailList[i].gender).SFDC_Value__c,
               //Status__c = wrpcls.ContactDetailList[i].sts,
               Status__c = Status__c.getvalues(wrpcls.ContactDetailList[i].sts).SFDC_Value__c,
               AccountID = AccountID,
               Preferred_Address_Type__c='jkl',
               Mail_Country__c='dfjgk');
               system.debug('con---->'+con);
               insert con;
               ContactId = con.id;
               //AccountId = AccountID;
               system.debug('&&&&&&&'+ ContactId);
               }                       
              }
              
              // Address Insert
              If(wrpcls.AddressDetailList != null && wrpcls.AddressDetailList .size()>0){
               for(Integer i=0;i<wrpcls.AddressDetailList.size();i++){
               string PreferredAddress = wrpcls.AddressDetailList[i].preferred_address;
               if(PreferredAddress == 'Y'){
               PreferredAddress = 'YES';}
               else if(PreferredAddress == 'N'){
               PreferredAddress = 'NO';}
               
               system.debug('PPPP-->'+PartnerId );
               //if(PartnerId != null)
               //ContactId = [Select id from Contact where Partner_Contact_ID__c =: PartnerId].Id;
               
               if(ContactId  != null){
                   system.debug('connnn'+ContactId);
                Contact_Address__c add = new Contact_Address__c(Contact__c = ContactId ,
                Preferred__c = PreferredAddress,
                 Address_Type__c = wrpcls.AddressDetailList[i].address_type,
                 //Address_Type__c = AddressType__c.getvalues(wrpcls.AddressDetailList[i].address_type).Value_in_SFDC__c,
                 Address_Line1__c = wrpcls.AddressDetailList[i].address_line,
                 City__c = wrpcls.AddressDetailList[i].city,
                 State__c = State__c.getvalues(wrpcls.AddressDetailList[i].state).SFDC_Value__c,
                 //State__c = wrpcls.AddressDetailList[i].state,
                 //Country__c = wrpcls.AddressDetailList[i].country,
                 Country__c = Country__c.getvalues(wrpcls.AddressDetailList[i].country).SFDC_Value__c,
                 Zipcode__c = wrpcls.AddressDetailList[i].zip_code,
                 //County__c = wrpcls.AddressDetailList[i].county,
                 //Status__c = wrpcls.AddressDetailList[i].sts);
                 Status__c = Status__c.getvalues(wrpcls.AddressDetailList[i].sts).SFDC_Value__c);
               system.debug('************'+add);
               insert add;
               }
               }
              }
          // Phone Insert
              If(wrpcls.TelephoneDetailList != null && wrpcls.TelephoneDetailList.size()>0){
               for(Integer i=0;i<wrpcls.TelephoneDetailList.size();i++){
               string PreferredTelephone = wrpcls.TelephoneDetailList[i].preferred_telephone;
               if(PreferredTelephone == 'Y'){
               PreferredTelephone = 'YES';}
               else if(PreferredTelephone == 'N'){
               PreferredTelephone = 'NO';}
               
               system.debug('PPPP-->'+PartnerId );
                             
               if(ContactId  != null){
                   system.debug('connnn'+ContactId);
                Contact_Phone__c ph = new Contact_Phone__c(Contact__c = ContactId ,
                //Partner_Contact_ID__c = wrpcls.TelephoneDetailList[i].partner_id,
                Preferred__c = PreferredTelephone,
                 Phone__c = wrpcls.TelephoneDetailList[i].telephone_number,
                 Phone_Type__c = wrpcls.TelephoneDetailList[i].telephone_type,
                 //Phone_Type__c = PhoneType__c.getvalues(wrpcls.TelephoneDetailList[i].telephone_type).Value_in_SFDC__c,
                // City__c = wrpcls.TelephoneDetailList[i].preferred_telephone,
                 Description__c = wrpcls.TelephoneDetailList[i].comments,
                 Country__c = Country__c.getvalues(wrpcls.TelephoneDetailList[i].country).SFDC_Value__c,
                 //Country__c = wrpcls.TelephoneDetailList[i].country,
                 //Status__c = wrpcls.TelephoneDetailList[i].sts);
                 Status__c = Status__c.getvalues(wrpcls.TelephoneDetailList[i].sts).SFDC_Value__c);
               system.debug('************'+ph);
               insert ph;
               }
               }
              }
              // Email Insert
              If(wrpcls.EmailDetailList != null && wrpcls.EmailDetailList.size()>0){
               for(Integer i=0;i<wrpcls.EmailDetailList.size();i++){
               string PreferredEmail = wrpcls.EmailDetailList[i].preferred_email;
               if(PreferredEmail == 'Y'){
               PreferredEmail = 'YES';}
               else if(PreferredEmail == 'N'){
               PreferredEmail = 'NO';}
               
               system.debug('PPPP-->'+PartnerId );
                             
               if(ContactId  != null){
                   system.debug('connnn'+ContactId);
                Contact_Email__c em = new Contact_Email__c(Contact__c = ContactId ,
                 Preferred__c = PreferredEmail,
                 Email_Type__c = wrpcls.EmailDetailList[i].email_type,
                 //Email_Type__c = EmailType__c.getvalues(wrpcls.EmailDetailList[i].email_type).Value_in_SFDC__c,
                 Email__c = wrpcls.EmailDetailList[i].email_address);
                 system.debug('************'+em);
                  insert em;
               }
               }
              }
              // Relationship Insert
              If(wrpcls.RelationShipDetailList != null && wrpcls.RelationShipDetailList.size()>0){
               for(Integer i=0;i<wrpcls.RelationShipDetailList.size();i++){
               system.debug('PPPP-->'+PartnerId );
                          
               if(ContactId  != null){
                   system.debug('connnn'+ContactId);
                Relationship__c rl = new Relationship__c(Contact__c = ContactId ,
                Account__c = AccountId,
                //Related_Partner_Id__c = wrpcls.RelationShipDetailList[i].related_partner_id,
                // Related_Name_Id__c = wrpcls.RelationShipDetailList[i].related_name_id,
                 Relationship_Type__c = wrpcls.RelationShipDetailList[i].relationship_type,
                 // Relationship_Type__c = RelationshipType__c.getvalues(wrpcls.RelationShipDetailList[i].relationship_type).Value_in_SFDC__c,
                 Start_Date__c = Date.valueof(wrpcls.RelationShipDetailList[i].start_date),
                 //Status__c = wrpcls.RelationShipDetailList[i].sts);
                 Status__c = Status__c.getvalues(wrpcls.RelationShipDetailList[i].sts).SFDC_Value__c);
               system.debug('************'+rl);
               insert rl;
               }
               }
              }
              }
              Endresult = 'Y';
          }       
          else
          {
            Endresult = 'No Account Found';
          }
          system.debug('Endresult----->'+Endresult);
          return Endresult;
          }
      
}